<!DOCTYPE html>
<script src=../../resources/testharness.js></script>
<script src=../../resources/testharnessreport.js></script>
<body>
  <!-- canvas cannot be arbitrarily small -->
  <canvas id='canvas'/>
</body>
<script>

var test = async_test('forces an error in the MediaRecorder API by changing ' +
    'the amount of tracks in the MediaStream, and checks that the correct ' +
    'sequence ondataavailable-stop-error is enforced');
var recorder;
var stream;

function drawSomethingOnCanvas(canvas) {
  // Drawing something on the canvas generates a frame in its captured stream.
  var context = canvas.getContext("2d");
  context.fillStyle = "red";
  context.fillRect(0, 0, 10, 10);
}

recorderOnDataAvailable = test.step_func(function(event) {
  recorder.onstop = recorderOnStop;

  // Add another track to the MediaStream to force an error.
  stream.addTrack(stream.getVideoTracks()[0].clone());
  drawSomethingOnCanvas(document.getElementById('canvas'));
});

recorderOnStop = test.step_func(function() {
  recorder.onstop = test.unreached_func('Unexpected stop event');
  recorder.ondataavailable = test.unreached_func('Unexpected data event');
  recorder.onerror = recorderOnError;
});

recorderOnError = test.step_func(function() {
  test.done();
});

var step = test.step_func(function() {
  var canvas = document.getElementById('canvas');
  stream = canvas.captureStream();
  assert_equals(stream.getVideoTracks()[0].readyState, 'live');

  recorder = new MediaRecorder(stream);

  recorder.onstop = test.unreached_func('Unexpected stop event');
  recorder.onpause = test.unreached_func('Unexpected pause event');
  recorder.onresume = test.unreached_func('Unexpected resume event');
  recorder.onerror = test.unreached_func('Unexpected error event');
  recorder.ondataavailable = recorderOnDataAvailable;

  recorder.start(0);
  drawSomethingOnCanvas(canvas);
});

step();

</script>
